package cn.edu.scau.cmi.liangzaoqing.client.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.liangzaoqing.hibernate.dao.MajorDAO;
import cn.edu.scau.cmi.liangzaoqing.hibernate.domain.Major;
import cn.edu.scau.cmi.liangzaoqing.hibernate.factory.ScauCmiHibernateSessionFactory;

public class HibernateClient5HibernateByHibernateFactory {

	public static void main(String[] args) {
		Major major=new Major();
		major.setName("土木工程");
		major.setId(5L);
		
		newMajor(major);
		listMajors();
	}

		public static void newMajor(Major major) {
			Session session = ScauCmiHibernateSessionFactory.getSession();
			Transaction transaction = session.beginTransaction();
//	 		(1) 直接使用Hibernate的CRUD功能		
			session.save(major);

			transaction.commit();
			session.close();
		}

	private static void listMajors() {
		MajorDAO MajorDAO=new MajorDAO();
		List Majors = MajorDAO.findAll();
		Iterator<?> MajorIterator = Majors.iterator();
		while(MajorIterator.hasNext()){
			Major Major = (Major) MajorIterator.next();
			System.out.println("调用DAO后的专业名是："+Major.getName()+"，调用DAO后的专业ID号是："+Major.getId());
		}

	}
	


}
